/**
 * 
 * 下拉树。4个参数：
 *obj：区域jquery对象
 *iniValue：初始值
 *rootId:树根节点 默认000000000000
 *level:显示的级别 默认5
 *loadSuccessCallback：初始加载完要执行的函数
 *onSelect：区域选择单击改变执行的函数
 *width:默认148
 */
function aeye_area_select(obj,rootId,iniValue,level,loadSuccessCallback,onSelect,width){
	if(!(obj instanceof jQuery)){
		obj = $("#"+obj);
	}
	if(!width){
		width=148;
	}
	var url = BASE_PATH + '/common/asyncAreaTree.html?1=1';
	if(level)
	{
	    url+="&level="+level;
	}
	var id = obj.attr("id");
	var contid = id+"_aeye_ztree";
	obj.attr("id",contid);
	obj = $("#"+contid);
	var menuContentId = id+"_areaSelect";
	var treeDemoId = id+"_treeDemo";
	var treeDiv = '<input type="hidden" name="'+id+'" id="'+id+'">'+
		'<div id="'+menuContentId+'" class="areaSelect" style="display:none; position: absolute;z-index:99;">'+
		'<ul id="'+treeDemoId+'" class="ztree" style="margin-top:0; width:'+width+'px;"></ul>'+
		'</div>';
	obj.after(treeDiv);
	var setting = {
		view: {
			dblClickExpand: false,
			autoCancelSelected:false,
			selectedMulti:false
		},
		data: {
			simpleData: {
				enable: true,
				idKey:"id",
				pIdKey:"pid"
			},
			key:{
				name:"name",
				id:"id"
			}
		},

		async: {
                enable: true,
                url: url,
                autoParam: ["id"]
        },

		callback:{
			onClick:onClick
		}
	};
	var hideObj = $('#'+id);
	var zNodes = [],zTree,topNodes;

	var firstUrl = url+"&needRoot=1";
    if(rootId){
        firstUrl+="&id="+rootId;
    }

	$.post(firstUrl,{},function(response){
		zNodes = response;
		$.fn.zTree.init($("#"+treeDemoId), setting, response);
		obj.bind("focus",showMenu).bind("keydown", searchNode).bind("input", inputinit);
		zTree = $.fn.zTree.getZTreeObj(treeDemoId);
		topNodes = zTree.getNodes();
		if(!iniValue){
			if(response!=null&&response.length>0){
				iniValue=response[0][setting.data.key.id];
			}
		}
		//console.log("iniValue:"+iniValue);
		if(iniValue){
			var initnode = zTree.getNodeByParam(setting.data.key.id,iniValue);
			zTree.selectNode(initnode);
			obj.val(initnode[setting.data.key.name]);
			hideObj.val(initnode[setting.data.key.id]);
		}

		if(loadSuccessCallback){
			loadSuccessCallback();
		}
	});

	function onClick(e, treeId, treeNode) {
		var nodes = zTree.getSelectedNodes(),
		v = "";
		//nodes.sort(function compare(a,b){return a.id-b.id;});
		//for (var i=0, l=nodes.length; i<l; i++) {
		//	v += nodes[i].name + ",";
		//}
		//if (v.length > 0 ) v = v.substring(0, v.length-1);
		//var cityObj = $("#citySel");
		//cityObj.attr("value", v);
		obj.val(nodes[0][setting.data.key.name]);
		hideObj.val(nodes[0][setting.data.key.id]);
		hideMenu();
		if(onSelect){
			onSelect();
		}
	}

	function showMenu(){
		var cityOffset = obj.offset();
		var left = cityOffset.left;
		var top = cityOffset.top + obj.outerHeight();
		$("#"+menuContentId).css({left:left + "px", top:top + "px"}).slideDown("fast");
		$("body").bind("mousedown", onBodyDown);

		zTree.expandNode(topNodes[0],true);//展开根节点
	}
	function onBodyDown(event) {
		if (!(event.target.id == menuContentId || $(event.target).parents("#"+menuContentId).length>0
				|| event.target.id == contid)) {
			hideMenu();
		}
	}
	function hideMenu() {
		$("#"+menuContentId).fadeOut("fast");
		$("body").unbind("mousedown", onBodyDown);
	}
	var nodeListHide = [], nodeListShow = [],nodeListShow1=[];
	function searchNode(e){
		if(e.keyCode!=13){
			return;
		}

		nodeListHide = zTree.getNodesByFilter(filter_hide);
		nodeListShow = zTree.getNodesByFilter(filter_show);
		handList(nodeListShow);
		zTree.hideNodes(nodeListHide);
		zTree.showNodes(nodeListShow1);
		//zTree.expandAll(true);
	}
	function inputinit(e){
		if($.trim(obj.val())==''){
			zTree.showNodes(nodeListHide);
			//$.fn.zTree.init($("#"+treeDemoId), setting, zNodes);
			//zTree.expandNode(topNodes[0],true);//展开根节点
		}
	}
	function handList(nodeListShow){
		nodeListShow1 = [];
		for(var i=0;i<nodeListShow.length;i++){
			parentHand(nodeListShow[i]);
		}
	}
	function parentHand(node){
		nodeListShow1.push(node);
		var pnode = node.getParentNode();
		if(pnode){
			parentHand(pnode);
		}
	}
	function filter_hide(node) {
		var tempval = obj.val();
		if(tempval=='' || tempval==null || tempval==undefined){
			return false;
		}else{
			return node[setting.data.key.name].indexOf(tempval)==-1;
		}
	}
	function filter_show(node) {
		var tempval = obj.val();
		if(tempval=='' || tempval==null || tempval==undefined){
			return true;
		}else{
			return node[setting.data.key.name].indexOf(tempval)!=-1;
		}
	}
}

function aeye_area_display(obj,onSelect,width,height){
	if(!(obj instanceof jQuery)){
		obj = $("#"+obj);
	}
	if(!width){
		width=160;
	}
	if(!height){
        height=480;
    }
	var url = BASE_PATH + '/common/asyncAreaTree.html';
	var id = obj.attr("id");
	var contid = id+"_aeye_ztree";
	obj.attr("id",contid);
	obj = $("#"+contid);
	var menuContentId = id+"_areaDisplay";
	var treeDemoId = id+"_treeDemo";
	var treeDiv =
		'<div id="'+menuContentId+'" class="areaDisplay" style="display:block;">'+
		'<ul id="'+treeDemoId+'" class="ztree" style="margin-top:0; width:'+width+'px;"></ul>'+
		'</div>';
	obj.html(treeDiv);
	var setting = {
		view: {
			dblClickExpand: false,
			autoCancelSelected:false,
			selectedMulti:false
		},
		data: {
			simpleData: {
				enable: true,
				idKey:"id",
				pIdKey:"pid"
			},
			key:{
				name:"name",
				id:"id"
			}
		},
		async: {
                enable: true,
                url: url,
                autoParam: ["id"]
        },
		callback:{
			onClick:onClick
		}
	};
	var zNodes = [],zTree,topNodes;

	var firstUrl = url+"?needRoot=1";

	$.post(firstUrl,{},function(response){
		zNodes = response;
		$.fn.zTree.init($("#"+treeDemoId), setting, response);
		zTree = $.fn.zTree.getZTreeObj(treeDemoId);
		topNodes = zTree.getNodes();
	});

	function onClick(e, treeId, treeNode) {
		var nodes = zTree.getSelectedNodes();
		obj.val(nodes[0][setting.data.key.name]);
		if(onSelect){
			onSelect(nodes[0][setting.data.key.id]);
		}
	}
}


function aeye_org_display(obj,onSelect,width,height){
	if(!(obj instanceof jQuery)){
		obj = $("#"+obj);
	}
	if(!width){
		width=160;
	}
	if(!height){
        height=480;
    }
	var url = BASE_PATH + '/common/asyncOrgTree.html';
	var id = obj.attr("id");
	var contid = id+"_aeye_ztree";
	obj.attr("id",contid);
	obj = $("#"+contid);
	var menuContentId = id+"_orgDisplay";
	var treeDemoId = id+"_treeDemo";
	var treeDiv =
		'<div id="'+menuContentId+'" class="orgDisplay" style="display:block;">'+
		'<ul id="'+treeDemoId+'" class="ztree" style="margin-top:0; width:'+width+'px;"></ul>'+
		'</div>';
	obj.html(treeDiv);
	var setting = {
		view: {
			dblClickExpand: false,
			autoCancelSelected:false,
			selectedMulti:false
		},
		data: {
			simpleData: {
				enable: true,
				idKey:"id",
				pIdKey:"pid"
			},
			key:{
				name:"name",
				id:"id"
			}
		},
		async: {
                enable: true,
                url: url,
                autoParam: ["id"]
        },
		callback:{
			onClick:onClick
		}
	};
	var zNodes = [],zTree,topNodes;

	var firstUrl = url+"?needRoot=1";

	$.post(firstUrl,{},function(response){
		zNodes = response;
		$.fn.zTree.init($("#"+treeDemoId), setting, response);
		zTree = $.fn.zTree.getZTreeObj(treeDemoId);
		topNodes = zTree.getNodes();
	});

	function onClick(e, treeId, treeNode) {
		var nodes = zTree.getSelectedNodes();
		obj.val(nodes[0][setting.data.key.name]);
		if(onSelect){
			onSelect(nodes[0][setting.data.key.id]);
		}
	}
}

var searchTable=function (tableId,searchFormId){
    var params = $('#'+tableId).bootstrapTable('getOptions');
    params.queryParams = function(params){
        var search={};

        $.each($('#'+searchFormId).serializeArray(),function(i,field){
            if(null != field.value && "" != field.value){
                params[field.name] = field.value;
            }
        })
        return params;
    }
    $('#'+tableId).bootstrapTable('refresh',params);
}

